package com.wyl.login.activity

import android.content.Intent
import android.view.View
import androidx.lifecycle.Observer
import com.wuhenzhizao.titlebar.widget.CommonTitleBar
import com.wyl.base.HomePage
import com.wyl.libbase.base.BindingActivity
import com.wyl.libbase.extend.toast
import com.wyl.libbase.utils.ActivityStack
import com.wyl.libbase.utils.CountDownUtils
import com.wyl.libbase.extend.openActivity
import com.wyl.login.R
import com.wyl.login.databinding.RegisterActivityBinding
import org.koin.android.viewmodel.ext.android.viewModel

class RegisterActivity : BindingActivity<RegisterActivityBinding>() {
    private val viewModel: LoginViewModel by viewModel()

    override fun getLayoutId(): Int = R.layout.register_activity

    override fun initView() {
        binding.vm = viewModel

        binding.titlebar.setListener { _, action, _ ->
            when (action) {
                CommonTitleBar.ACTION_LEFT_BUTTON -> onBackPressed()
                else -> {
                }
            }
        }
    }

    override fun loadData() {
        viewModel.codeSuccess.observe(this, Observer {
            toast(it ?: "")
            CountDownUtils.changeBtnUi(
                binding.tvGetCode,
                60,
                this.getString(R.string.ui_code_obtain)
            )
        })

        viewModel.success.observe(this, Observer {
            toast(it ?: "")
            ActivityStack.screenManager.popAllActivitys()
            openActivity(HomePage)
        })

        viewModel.error.observe(this, Observer {
            toast(it ?: "")
        })

    }

    override fun onClick(v: View) = when (v.id) {
        R.id.tv_agreement -> startActivity(Intent(this, AgreementActivity::class.java))
        else -> {
        }
    }

}
